package com.nave.segundaguerra.servidorecliente.cliente;


import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PointF;
import android.widget.RelativeLayout;

import com.example.honraepodercliente.R;
import com.nave.segundaguerra.game.Animation;
import com.nave.segundaguerra.game.SpritePlayer;
import com.nave.segundaguerra.game.WrapMode;
import com.nave.segundaguerra.game.uniqueAnimation;
import com.nave.segundaguerra.servidorecliente.util.Const;
import com.nave.segundaguerra.servidorecliente.util.Protocolo;
import com.nave.segundaguerra.servidorecliente.util.RectLibrary;
import com.nave.segundaguerra.servidorecliente.util.Tools;


public class PlayerCliente 
{
	public PointF position = new PointF();
	
	
    private uniqueAnimation playerUniqueAnimation;
    private Animation flameAnimation;
    
	protected int x;
	protected int y;
	protected int plusScrollX,plusScrollY;
	protected Paint paint;
	protected boolean telaCheia = false;
	
	public DadosDoCliente clientData;
	
	private Bitmap imagemPlayer;
	
	protected String time;
	
	protected SpritePlayer playerSprite;
	
	protected String nome;
	private String minhaClasse;
	
	public PlayerCliente(){
		
	}
	public void Update()
	{

	}
	
	public PlayerCliente(String nome, Point position, String minhaClasse)
	{
		playerUniqueAnimation = new uniqueAnimation();
    	flameAnimation = new Animation(null,null);
    	
    	Resources r = Tools.getResource();
    	
		this.x = 200;
		this.y = 200;
    	
    	Bitmap[] flameBitmap = new Bitmap[]
    	{
    		BitmapFactory.decodeResource(r, R.drawable.assault01),
    		BitmapFactory.decodeResource(r, R.drawable.assault02),
    		BitmapFactory.decodeResource(r, R.drawable.assault03),
    		BitmapFactory.decodeResource(r, R.drawable.assault04)
    	};
    	
		playerUniqueAnimation.Sprites = flameBitmap;
		playerUniqueAnimation.spriteRect = RectLibrary.getInstance().getRectFromBitmap("Player",flameBitmap[0], 1.5f);
		playerUniqueAnimation.timeRating = 1;
		playerUniqueAnimation.offset = 1.5f;
		playerUniqueAnimation.animationType = WrapMode.ONCE;
		playerUniqueAnimation.animationName = "Player";
		playerUniqueAnimation.spriteRect.set((int)(x- playerUniqueAnimation.Sprites[0].getWidth()/playerUniqueAnimation.offset), (int)(y - playerUniqueAnimation.Sprites[0].getHeight()/playerUniqueAnimation.offset), (int)(x + playerUniqueAnimation.Sprites[0].getWidth()/playerUniqueAnimation.offset), (int)(y + playerUniqueAnimation.Sprites[0].getHeight()/playerUniqueAnimation.offset));
		flameAnimation.allAnimations.add(playerUniqueAnimation);
		flameAnimation.StartAnimation("Player");
    	flameAnimation.switchFrame(0);
		
		paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setTextSize(20);
		this.minhaClasse = minhaClasse;
		this.nome = nome;

		
		if(this.minhaClasse != "General"){
		playerSprite = new SpritePlayer(minhaClasse, 1, 3);
		}
		atualizarPaint();
	}
	
	public void draw(Canvas canvas)
	{
		flameAnimation.Draw(canvas, playerUniqueAnimation.spriteRect);
	}
	
	public DadosDoCliente getDadosCliente(){
		return this.clientData;
	}
	public void setDadosCliente(DadosDoCliente dados){
		this.clientData = dados;
	}
	public void CarregarImagem(int larguraTela, int alturaTela){
		
	}
	public String getNome() {
		return nome;
	}
	public void setNome(String nome) {
		this.nome = nome;
	}
	
	public Bitmap getImage(){
		return imagemPlayer;
	}
	public void setImage(Bitmap bitmap){
		this.imagemPlayer = bitmap;
	}
	
	public String getTime(){
		return this.time;
	}
	public void setTime(String time){
		this.time = time;
		atualizarPaint();
	}
	private void atualizarPaint(){
		if(this.time == Const.TIMEVERMELHO)
		{
			this.paint.setColor(Color.RED);
		}else 
			if(this.time == Const.TIMEAZUL)
		{
			this.paint.setColor(Color.BLUE);
		}else{
			this.paint.setColor(Color.WHITE);
		}
	}
	
	public String toString() {
		return "Jogador [nome=" + nome + ", x=" + x + ", y=" + y + "]";
	}
	public String toStringCSV() {
		return nome + "," + x + "," + y + ";";
	}
	
	public Point getPosition() {
		return new Point(this.x, this.y);
	}
	public void setPosition(Point pos) {
	this.x = pos.x;
	this.y = pos.y;
		if(this.minhaClasse != "General"){
		playerSprite.setPosition(pos);
		}
	}
	
	public int getScrollX(){
		return this.plusScrollX;
	}
	public int getScrollY(){
		return this.plusScrollY;
	}
	public void sendDadosIniciais(){
		clientData.getCliente().Send(Protocolo.PROTOCOL_ID + "," + this.getNome() + ","+ this.getPosition().x + "," + this.getPosition().y 
				+ "," + this.getTime() + "," + this.getMinhaClasse());
	}
	public String getMinhaClasse(){
		return this.minhaClasse;
	}
	public boolean verificarTelaCheia(){
		return this.telaCheia;
	}
	public void verificarZoom(float x, float y, int altura, int largura){
	}
	
	public void sendTiro(DadosDoCliente dadosCliente, Point toque){
	}
	public void sendPos(Point pos){
	}
	public void dirViewPort(Point pnt1, Point pnt2, Point tamCanvas, RelativeLayout container){
	}
}